package group2.controller;

import group2.model.*;
import group2.service.IBookService;
import group2.service.IShoppingCarService;
import group2.utils.JWTUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shopingcar")
public class ShoppingCarController {
    static Logger logger=Logger.getLogger(ShoppingCarController.class);
    @Autowired
    IShoppingCarService shoppingCarService;
    @Autowired
    IBookService bookService;
    /**1.用户登录时，数据库操作
     * 增加商品数量
     * */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Map<String,Object> addToCar(@RequestParam("book_id") Integer book_id, HttpServletRequest request){
        logger.debug("参数"+book_id);
        Map<String,Object> map=new HashMap<>();
        int i;
       // UserInfo logUser = (UserInfo) session.getAttribute("loguser");
       // BooksCar booksCar = shoppingCarService.selectShoppingCarId(book_id,logUser.getUserId());
        String token = request.getHeader("token");
        //获取用户编号
        String userId = JWTUtil.getTokenInfo(token).getClaim("userId").asString();
        BooksCar booksCar = shoppingCarService.selectShoppingCarId(book_id,userId);
        if (booksCar==null){
            //如果不存在，则新建一个购物车
            i= shoppingCarService.insertShopCar(book_id, userId);
        }else {
            //如果已经有，那就更新数据
            i = shoppingCarService.addShopCarBook(book_id, userId);
        }
        if (i==1){
            map.put("result",true);
        }else {
            map.put("result",false);
            map.put("message","数量增加失败");
        }
        return map;
    }
    /*}*/
    /**
     * 删除购物车中的某商品的数量
     * */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String,Object> deleteToCar(@RequestParam("book_id") Integer book_id,HttpServletRequest request){
        logger.debug("删除商品："+book_id);
        Map<String,Object> map=new HashMap<>();
        String token = request.getHeader("token");
        //获取用户编号
        String userId = JWTUtil.getTokenInfo(token).getClaim("userId").asString();
        BooksCar booksCar = shoppingCarService.selectShoppingCarId(book_id, userId);
        int i=0;
        if (booksCar.getBook_num()>1){//商品数量减少最低不能为0
            i = shoppingCarService.removeShopCar(book_id, userId);
        }
        if (i==1){
            map.put("result",true);
        }else {
            map.put("result",false);
            map.put("message","数量不能低于1");
        }
        return map;
    }
    /**
     * 删除购物车某商品
     * */
    @RequestMapping(value = "/removeGoods")
    @ResponseBody
    public Map<String,Object> removeCar(@RequestParam("book_id") Integer book_id, HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        String token = request.getHeader("token");
        //获取用户编号
        String userId = JWTUtil.getTokenInfo(token).getClaim("userId").asString();
        int i = shoppingCarService.deleteShopCar(book_id,userId);
        if (i==1){
            map.put("result",true);
        }else {
            map.put("result",false);
            map.put("message","删除失败");
        }
        return map;
    }

    /**
     * 清空用户购物车信息
     * */
    @RequestMapping(value = "/deleteAllCar")
    @ResponseBody
    public Map<String,Object> deleteAllCar(HttpServletRequest request ){
        Map<String,Object> map=new HashMap<>();
        String token = request.getHeader("token");
        //获取用户编号
        String userId = JWTUtil.getTokenInfo(token).getClaim("userId").asString();
        int i = shoppingCarService.deleteAll(userId);
        if (i==1){
            map.put("result",true);
        }else {
            map.put("result",false);
        }
        return map;

    }

    /**
     * 查询用户购物车信息
     * */
    @RequestMapping(value = "/selectShopCar")
    @ResponseBody
    public List selectShopCar(ModelMap modelMap, HttpServletRequest request ){
        /*UserInfo user = (UserInfo) session.getAttribute("userInfo");
        if (user==null){
            return "redirect:/index.jsp";
        }
        else {
            //String user_id = user.getUser_id();*/
        String token = request.getHeader("token");
        //获取用户编号
        String userId = JWTUtil.getTokenInfo(token).getClaim("userId").asString();
        List<BooksCar> carInfo = shoppingCarService.findCarInfo(userId);
        modelMap.addAttribute("carInfo",carInfo);
        return carInfo;
        /*}
         */
    }
}




